package com.meetingshow.meeting.uiservice;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.meetingshow.common.base.service.model.QueryResults;
import com.meetingshow.common.base.uiservice.BaseUIServiceImp;
import com.meetingshow.common.base.uiservice.exception.BaseUIServiceException;
import com.meetingshow.common.validator.RemoveValidator;
import com.meetingshow.meeting.dao.model.UserFollowPO;
import com.meetingshow.meeting.service.IUserFollowService;
import com.meetingshow.meeting.service.model.UserFollowQueryParas;
import com.meetingshow.meeting.uiservice.model.MeetingVO;
import com.meetingshow.meeting.uiservice.model.UserFollowQueryParasVO;
import com.meetingshow.meeting.uiservice.model.UserFollowVO;
import com.meetingshow.privilege.uiservice.model.UserVO;
import com.meetingshow.utils.BaseUtil;

public class UIUserFollowServiceImp extends BaseUIServiceImp implements
        IUIUserFollowService {
    
    public final static Logger logger = Logger.getLogger(UIUserFollowServiceImp.class);

    private IUserFollowService userFollowService;
    
    /**
     * @return the userFollowService
     */
    public IUserFollowService getUserFollowService() {
        return userFollowService;
    }

    /**
     * @param userFollowService the userFollowService to set
     */
    public void setUserFollowService(IUserFollowService userFollowService) {
        this.userFollowService = userFollowService;
    }

    @Override
    public Integer saveUserFollow(UserFollowVO userFollowVO)
            throws BaseUIServiceException {
        
        if (!BaseUtil.objectNotNull(userFollowVO)) {
            this.throwValidaterException("userFollowVO is null.");
        }
        
        userFollowVO.setCreateTime(new Date());
        
        validateThrowEx(userFollowVO);
        
        UserFollowQueryParas userFollowQueryParas = new UserFollowQueryParas();
        userFollowQueryParas.setMeetingId_e(String.valueOf(userFollowVO.getMeeting().getMeetingId()));
        userFollowQueryParas.setUserId_e(String.valueOf(userFollowVO.getUser().getUserId()));
        
        QueryResults<UserFollowPO> ufQueryResults = null;
        
        try {
            ufQueryResults = getUserFollowService().queryUserFollow(userFollowQueryParas);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        if (QueryResults.resultsListNotNull(ufQueryResults)) {
            logger.error("The user had followed this meeting before.");
            throw new BaseUIServiceException("The user had followed this meeting before.");
        }
        
        try {
            Integer id = getUserFollowService().saveUserFollow(userFollowVO.toFollowPO());
            userFollowVO.setId(id);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        return userFollowVO.getId();
    }

    @Override
    public void removeUserFollow(UserFollowVO userFollowVO)
            throws BaseUIServiceException {
        if (!BaseUtil.objectNotNull(userFollowVO)) {
            this.throwValidaterException("userFollowVO is null.");
        }
        
        validateThrowEx(userFollowVO, RemoveValidator.class);
        
        UserFollowQueryParas userFollowQueryParas = new UserFollowQueryParas();
        userFollowQueryParas.setId_e(String.valueOf(userFollowVO.getId()));
        
        QueryResults<UserFollowPO> userFollowQueryResults = null;
        try {
            userFollowQueryResults = getUserFollowService().queryUserFollow(userFollowQueryParas);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        if (QueryResults.resultsListNotNull(userFollowQueryResults)) {
            UserFollowPO persistUserFollowPO = userFollowQueryResults.getResultList().get(0);
            try {
                getUserFollowService().removeUserFollow(persistUserFollowPO);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                throw new BaseUIServiceException(e);
            }
        }
    }

    @Override
    public QueryResults<UserFollowVO> queryUserFollow(
            UserFollowQueryParasVO userFollowQueryParasVO)
            throws BaseUIServiceException {
        if (!BaseUtil.objectNotNull(userFollowQueryParasVO)) {
            this.throwValidaterException("userFollowQueryParasVO is null.");
        }
        
        validateThrowEx(userFollowQueryParasVO);
        
        QueryResults<UserFollowPO> userFollowPOQueryResults = null;
        try {
            userFollowPOQueryResults = getUserFollowService().queryUserFollow(userFollowQueryParasVO.toUserFollowQueryParas());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        QueryResults<UserFollowVO> userFollowVOQueryResults = new QueryResults<UserFollowVO>();
        List<UserFollowVO> userFollowVOList = new ArrayList<UserFollowVO>();
        if (QueryResults.resultsListNotNull(userFollowPOQueryResults)) {
            for (UserFollowPO userFollowPO : userFollowPOQueryResults.getResultList()) {
                if (BaseUtil.objectNotNull(userFollowPO)) {
                    userFollowVOList.add(new UserFollowVO(userFollowPO));
                }
            }
        }
        
        userFollowVOQueryResults.setFirstResultNo(userFollowPOQueryResults.getFirstResultNo());
        userFollowVOQueryResults.setResultsPerGroup(userFollowPOQueryResults.getResultsPerGroup());
        userFollowVOQueryResults.setResultList(userFollowVOList);
        userFollowVOQueryResults.setTotalResultsNum(userFollowPOQueryResults.getTotalResultsNum());
        
        return userFollowVOQueryResults;
    }

    @Override
    public List<UserVO> getUsersFlwMeeting(Integer meetingId)
            throws BaseUIServiceException {
        if (meetingId == null) {
            this.throwValidaterException("meetingId is null.");
        }
        
        UserFollowQueryParas userFollowQueryParas = new UserFollowQueryParas();
        userFollowQueryParas.setMeetingId_e(String.valueOf(meetingId));
        
        QueryResults<UserFollowPO> userFollowPOQueryResults = null;
        try {
            userFollowPOQueryResults = getUserFollowService().queryUserFollow(userFollowQueryParas);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        List<UserVO> userList = new ArrayList<UserVO>();
        
        if (QueryResults.resultsListNotNull(userFollowPOQueryResults)) {
            for (UserFollowPO userFollowPO : userFollowPOQueryResults.getResultList()) {
                if (userFollowPO.getUser() != null) {
                    userList.add(new UserVO(userFollowPO.getUser()));
                }
            }
        }
        
        return userList;
    }

    @Override
    public List<MeetingVO> getMeetingsFlwdByUser(Integer userId)
            throws BaseUIServiceException {
        if (userId == null) {
            this.throwValidaterException("userId is null.");
        }
        
        UserFollowQueryParas userFollowQueryParas = new UserFollowQueryParas();
        userFollowQueryParas.setUserId_e(String.valueOf(userId));
        
        QueryResults<UserFollowPO> userFollowPOQueryResults = null;
        try {
            userFollowPOQueryResults = getUserFollowService().queryUserFollow(userFollowQueryParas);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        List<MeetingVO> meetingList = new ArrayList<MeetingVO>();
        
        if (QueryResults.resultsListNotNull(userFollowPOQueryResults)) {
            for (UserFollowPO userFollowPO : userFollowPOQueryResults.getResultList()) {
                if (userFollowPO.getMeeting() != null) {
                    meetingList.add(new MeetingVO(userFollowPO.getMeeting()));
                }
            }
        }
        
        return meetingList;
    }
}
